package com.lp.controller.iot;

import com.lp.bo.IotVisualMoudleInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.controller.BaseController;
import com.lp.util.ObjectUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author thomas.he
 * @Description:
 * @date 2021/7/27 21:38
 */
@Controller
public class IotVisualMoudleInfoController extends BaseController
{
    @RequestMapping(method={org.springframework.web.bind.annotation.RequestMethod.POST}, value={"/page/moudle/visual"})
    public ModelAndView selectPage(HttpServletResponse response, @RequestHeader(value="USER-KEY", required=true) String userKey, @RequestBody IotVisualMoudleInfoBO obj, @RequestParam(required=false) Integer pageSize, @RequestParam Integer paged)
    {
        Map resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            if (ObjectUtil.isNotEmpty(obj.getMoudle_type()).booleanValue()) {
                obj.setUser_id(user.getId());
            }
            resultMap = this.service.selectPageList("IotVisualMoudleInfo.selectPage", getPageBean(paged, pageSize), obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[] { obj });
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method={org.springframework.web.bind.annotation.RequestMethod.POST}, value={"/moudle/visual"})
    public ModelAndView save(HttpServletResponse response, @RequestHeader(value="USER-KEY", required=true) String userKey, @RequestBody IotVisualMoudleInfoBO obj)
    {
        Map resultMap = getResultMap();
        try {
            UserInfoBO user = getUserInfoByUserKey(userKey);
            obj.setUser_id(user.getId());
            resultMap = this.service.insert("IotVisualMoudleInfo.insert", obj, new Object[0]);
            putData(resultMap, obj);
        } catch (Exception e) {
            exception(e, resultMap, new Object[] { obj });
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method={org.springframework.web.bind.annotation.RequestMethod.GET}, value={"/moudle/visual"})
    public ModelAndView selectOne(HttpServletResponse response, @RequestParam Integer id)
    {
        Map resultMap = getResultMap();
        try {
            resultMap = this.service.selectOne("IotVisualMoudleInfo.selectOne", new IotVisualMoudleInfoBO(id), new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[] { id });
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method={org.springframework.web.bind.annotation.RequestMethod.PUT}, value={"/moudle/visual"})
    public ModelAndView update(HttpServletResponse response, @RequestBody IotVisualMoudleInfoBO obj)
    {
        Map resultMap = getResultMap();
        try {
            resultMap = this.service.update("IotVisualMoudleInfo.update", obj, new Object[0]);
        } catch (Exception e) {
            exception(e, resultMap, new Object[] { obj });
        }
        return getModelAndView(response, resultMap);
    }

    @RequestMapping(method={org.springframework.web.bind.annotation.RequestMethod.DELETE}, value={"/moudle/visual"})
    public ModelAndView delete(HttpServletResponse response, @RequestParam Integer id)
    {
        Map resultMap = getResultMap();
        try {
            IotVisualMoudleInfoBO obj = new IotVisualMoudleInfoBO();
            if (ObjectUtil.isEmpty(id).booleanValue()) {
                putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR.intValue());
            }
            obj.setId(id);
            obj.setDelete_flag(Constants.DELETE.YES);
            resultMap = this.service.update("IotVisualMoudleInfo.update", obj, new Object[0]);
        }
        catch (Exception e) {
            exception(e, resultMap, new Object[] { id });
        }
       return getModelAndView(response, resultMap);
    }
}
